
#include "communication.h"
#include "sim7600ce.h"
#include "lora.h"
#include "common.h"
#include "config.h"
#include <string.h>
#include "sense_manager.h"

static uint8_t ReceCmd(uint8_t *pBuffer,uint16_t len);

int ReportFormat(ReportCode_TypeDef function, uint8_t *pBuffer, uint8_t len, TickType_t pportMAX_DELAY)
{
	uint16_t CrcData;
	uint8_t i;
	PT_SendDataStruct ptSendData;
	
	ptSendData = (PT_SendDataStruct)pvPortMalloc(sizeof(T_SendDataStruct));
	ptSendData->pucSendDataBuf = (uint8_t *)pvPortMalloc(len + 6 + 4);
	ptSendData->Len = len + 6 + 4;
	
	ptSendData->pucSendDataBuf[0] = Machine_Addr[0];
	ptSendData->pucSendDataBuf[1] = Machine_Addr[1];
	ptSendData->pucSendDataBuf[2] = function;
	ptSendData->pucSendDataBuf[3] = len;
	for(i=0;i<len;i++)
	{
		ptSendData->pucSendDataBuf[4+i] = pBuffer[i];
	}
	CrcData = MBCrcCheck(&ptSendData->pucSendDataBuf[4],len);
	ptSendData->pucSendDataBuf[len+4] = CrcData >> 8;
	ptSendData->pucSendDataBuf[len+5] = CrcData;
	for(i=0;i<4;i++)
	{
		ptSendData->pucSendDataBuf[len+6+i] = 0xFE;
	}
	
	return xQueueSend(SendMessage_Queue, &ptSendData, pportMAX_DELAY);
}


int ReportPowerFormat(uint8_t *pBuffer, uint8_t len, TickType_t pportMAX_DELAY)
{
	uint8_t i;
	PT_SendDataStruct ptSendData;
	
	ptSendData = (PT_SendDataStruct)pvPortMalloc(sizeof(T_SendDataStruct));
	ptSendData->pucSendDataBuf = (uint8_t *)pvPortMalloc(len + 3);
	ptSendData->Len = len + 3;
	
	ptSendData->pucSendDataBuf[0] = Machine_Addr[0];
	ptSendData->pucSendDataBuf[1] = Machine_Addr[1];
	ptSendData->pucSendDataBuf[2] = RPC_Power_Data;
	ptSendData->pucSendDataBuf[3] = pBuffer[0] + 2;
	for(i=1;i<len;i++)
	{
		ptSendData->pucSendDataBuf[3+i] = pBuffer[i];
	}
	return xQueueSend(SendMessage_Queue, &ptSendData, pportMAX_DELAY);
}
int ReportHeartFormat(TickType_t pportMAX_DELAY)
{
	uint8_t i;
	PT_SendDataStruct ptSendData;
	
	ptSendData = (PT_SendDataStruct)pvPortMalloc(sizeof(T_SendDataStruct));
	ptSendData->pucSendDataBuf = (uint8_t *)pvPortMalloc(10);
	ptSendData->Len = 10;

	ptSendData->pucSendDataBuf[0] = Machine_Addr[0];
	ptSendData->pucSendDataBuf[1] = Machine_Addr[1];
	ptSendData->pucSendDataBuf[2] = RPC_HeartBeat;
	ptSendData->pucSendDataBuf[3] = 0x00;
	ptSendData->pucSendDataBuf[4] = 0x00;
	ptSendData->pucSendDataBuf[5] = 0x00;
	for(i=0;i<4;i++)
	{
		ptSendData->pucSendDataBuf[6+i] = 0xFE;
	}
	return xQueueSend(SendMessage_Queue, &ptSendData, pportMAX_DELAY);
}
void ReportNull(void)
{
	uint8_t i;
	PT_SendDataStruct ptSendData;
	
	ptSendData = (PT_SendDataStruct)pvPortMalloc(sizeof(T_SendDataStruct));
	ptSendData->pucSendDataBuf = (uint8_t *)pvPortMalloc(4);
	ptSendData->Len = 4;
	for(i=0;i<4;i++)
	{
		ptSendData->pucSendDataBuf[i] = 0xFE;
	}
	SIM7600CESend(ptSendData->pucSendDataBuf, ptSendData->Len, 1000);
	vPortFree(ptSendData->pucSendDataBuf);
	vPortFree(ptSendData);
}

const char FrameEnd[] = {0xFE,0xFE,0xFE,0xFE,0x00};

void ReceSperate(uint8_t *pBuffer)
{
	uint8_t i,j;
	uint16_t SperateLen,Len;
	uint8_t ucSperateNum = 0;
	uint8_t *pEndPoint,*pStartPoint,*pSperateCount[10];
	
	Len = SIM7600CE_BUFFLEN;
	pStartPoint = pBuffer;
	pSperateCount[0] = pBuffer;
	do
	{
		pEndPoint = (uint8_t *)mystrstr((char *)pStartPoint, (char *)FrameEnd, Len);
		if(pEndPoint)
		{
			ucSperateNum++;
			pSperateCount[ucSperateNum] = pEndPoint + 4;
			Len -= (pSperateCount[ucSperateNum] - pStartPoint);
			pStartPoint = pEndPoint + 4;
		}
	}
	while(pEndPoint);
	
	for(i = 0; i < ucSperateNum; i++)
	{
		SperateLen = pSperateCount[i + 1] - pSperateCount[i];
		j = 0;
		while(*(pSperateCount[i + 1] + j) == 0xFE)
		{
			j++;
			SperateLen++;
		}
		ucSperateNum -= (j / 4);
		pSperateCount[i + 1] = pSperateCount[i + 1] + j;
		ReceCmd(pSperateCount[i], SperateLen);
		//LoraSend(pSperateCount[i], SperateLen, 2000);
	}
}
	
static uint8_t ReceCmd(uint8_t *pBuffer,uint16_t len) 
{
	if((len - 10) == pBuffer[3])
	{
		if(ReceCrcCheck(&pBuffer[4],pBuffer[3] + 2))
		{
			if((pBuffer[0] == Machine_Addr[0]) && (pBuffer[1] == Machine_Addr[1]))
			{
				switch(pBuffer[2])
				{
					case RPC_Machine_Register:
						xEventGroupSetBits(TCPEventGroupHandler, MACHINE_REGISTERBIT);
						break;
					case RPC_InfraredTemp_Data:
						xEventGroupSetBits(TCPEventGroupHandler, INFRAREDTEMP_DATABIT);
						GetSenseOpr("infraredtemp")->MemMalloc(&pBuffer[4],pBuffer[3]);
						break;
					case RPC_Vibrator_Data:
						xEventGroupSetBits(TCPEventGroupHandler, VIBRATOR_DATABIT);
						GetSenseOpr("vibrator")->MemMalloc(&pBuffer[4],pBuffer[3]);
						break;
					case RPC_Location_Data:
						xEventGroupSetBits(TCPEventGroupHandler, LBS_REGISTERBIT);
						break;
				}
				return 1;
			}
		}
	}
	else if(pBuffer[2] == RPC_HeartBeat)
	{
		xEventGroupSetBits(TCPEventGroupHandler, HEARTBEATBIT);
		ReportHeartFormat(2000);
		return 1;
	}
	else if(pBuffer[2] == RPC_Power_Data)
	{
		if((pBuffer[0] == Machine_Addr[0]) && (pBuffer[1] == Machine_Addr[1]))
		{
			xEventGroupSetBits(TCPEventGroupHandler, POWER_DATABIT);
			LoraSend(&pBuffer[4], pBuffer[3] + 9, 2000);
			return 1;
		}
	}
	return 0;
}
